package com.he.architecture.controller;

import com.he.architecture.Enums.CustomerOrderTypeEnums;
import com.he.architecture.common.controller.LogicController;
import com.he.architecture.dao.BusinessDao;
import com.he.architecture.dto.*;
import com.he.architecture.dto.EntityDtos.*;
import com.he.architecture.entity.*;
import com.he.architecture.service.BusinessService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Nullable;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/business")
public class BusinessController extends LogicController<BusinessService, BusinessDao, Business,Long> {
    /** DTO转化器 */
    @Resource
    private ModelMapper modelMapper;
    public BusinessController(@Autowired BusinessService service){
        super(service);
    }


    @PutMapping("/createShop")
    public ResponseResult createShop(@RequestBody CreateShopRequestDto shopRequestDto){
        Set<Shop> shops= getService().createShop(shopRequestDto.getBus_id(),shopRequestDto.getShop());
        Set<ShopResponse> shopResponses=shops.stream().map(shop->modelMapper.map(shop,ShopResponse.class)).collect(Collectors.toSet());
        Map<String,Object> map=new HashMap<>();
        map.put("shopResponses",shopResponses);
        return new ResponseResult(200,map);
    }
    @PutMapping("/createShopItem")
    public ResponseResult createShopItem(@RequestBody CreateShopItemRequestDto shopItemRequestDto){
        Set<ShopItem> shopItems= getService().createShopItem(shopItemRequestDto.getBus_id(),shopItemRequestDto.getShopItem());
        Set<ShopItemResponse> shopItemResponses=shopItems.stream().map(shop->modelMapper.map(shop,ShopItemResponse.class)).collect(Collectors.toSet());
        Map<String,Object> map=new HashMap<>();
        map.put("shopItemResponses",shopItemResponses);
        return new ResponseResult(200,map);

    }
    @PutMapping("/createItem")
    public ResponseResult createItem(@RequestBody CreateItemRequestDto itemRequestDto){
        Set<Item> items= getService().createItem(itemRequestDto.getBus_id(),itemRequestDto.getShop_id(),itemRequestDto.getItem());
        Set<ItemResponse> itemResponses=items.stream().map(shop->modelMapper.map(shop, ItemResponse.class)).collect(Collectors.toSet());
        Map<String,Object> map=new HashMap<>();
        map.put("itemResponses",itemResponses);
        return new ResponseResult(200,map);
    }
    @GetMapping("/getShopOrders")
    public ResponseResult getCustomerOrderList(@RequestParam("bus_id")Long bus_id,@RequestParam("shop_id") @Nullable Long shop_id){
        List<ShopWithOrdersResponseDto> customerOrderList = getService().getCustomerOrderList(bus_id, shop_id);

        Map<String,Object> map=new HashMap<>();
        map.put("customerOrderList",customerOrderList);
        return new ResponseResult(200,map);
    }
    @PostMapping("/updateOrder")
    public ResponseResult updateOrder(@RequestBody UpdateOrderRequestDto orderRequestDto){
        Set<CustomerOrder> customerOrders= getService().customerOrderTypeChange(orderRequestDto.getBus_id(),orderRequestDto.getShop_id(),orderRequestDto.getCustomerOrder(), CustomerOrderTypeEnums.valueOf(orderRequestDto.getOperationType()));
        Set<CustomerOrderResponse> customerOrderResponses=customerOrders.stream().map(customerOrder->modelMapper.map(customerOrder,CustomerOrderResponse.class)).collect(Collectors.toSet());
        Map<String,Object> map=new HashMap<>();
        map.put("customerOrderResponses",customerOrderResponses);
        return new ResponseResult(200,map);
    }
}
